﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

namespace WhatSNew.AppCode
{
    public class UserInfoAccess: IUserInfo
    {
        /// <summary>
        /// 判断userID是否存在于数据库中
        /// </summary>
        /// <param name="userID"></param>
        /// <returns>不存在返回false，否则返回true</returns>
        public bool IsExist(int userID)
        {
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandStr = "SELECT FROM UserPInfo WHERE ID = " + userID.ToString();
            SqlCommand sqlCom = new SqlCommand(commandStr, con);
            SqlDataReader sqlRead = sqlCom.ExecuteReader();
            return sqlRead.HasRows;
        }

        /// <summary>
        /// 判断用户名是否存在
        /// </summary>
        /// <param name="userName"></param>
        /// <returns>存在返回其ID，否则返回-1</returns>
        public int IsExist(string userName)
        {
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandStr = "SELECT ID FROM UserPInfo WHERE Name = '" + userName + "'";
            SqlCommand sqlCom = new SqlCommand(commandStr, con);
            SqlDataReader sqlRead = sqlCom.ExecuteReader();
            if (sqlRead.HasRows)
            {
                sqlRead.Read();
                int id = (int)sqlRead["ID"];
                con.Close();
                return id;
            }
            else
            {
                con.Close();
                return -1;
            }
        }


        /// <summary>
        /// 删除userID的用户管理信息
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public bool DeleteMInfo( int userID )
        {
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandStr = "DELETE FROM UserMInfo WHERE ID = " + userID.ToString();
            SqlCommand sqlCom = new SqlCommand(commandStr, con);
            try
            {
                sqlCom.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception)
            {
                return false;
            }
            finally
            {
                con.Close();
            }
        }

        /// <summary>
        /// 删除userID的个人信息
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public bool DeletePInfo( int userID )
        {
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandStr = "DELETE FROM UserPInfo WHERE ID = " + userID.ToString();
            SqlCommand sqlCom = new SqlCommand(commandStr, con);
            try
            {
                sqlCom.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception)
            {
                return false;
            }
            finally
            {
                con.Close();
            }
        }

        /// <summary>
        /// 删除特定的用户关系
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="followID"></param>
        /// <returns></returns>
        public bool DeleteRelation( int ID, int followID )
        {
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandStr = "DELETE FROM UserRelation WHERE ID = " + ID.ToString()
                + "AND FollowID = " + followID.ToString();
            SqlCommand sqlCom = new SqlCommand(commandStr, con);
            try
            {
                sqlCom.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception)
            {
                return false;
            }
            finally
            {
                con.Close();
            }
        }

        /// <summary>
        /// 删除userID的所有关注信息
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public bool DeleteRelation( int userID )
        {
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandStr = "DELETE FROM UserRelation WHERE ID = " + userID.ToString()
                    + "OR FollowID = " + userID.ToString();
            SqlCommand sqlCom = new SqlCommand(commandStr, con);
            try
            {
                sqlCom.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception)
            {
                return false;
            }
            finally
            {
                con.Close();
            }
        }

        /// <summary>
        /// 删除用户ID的信息
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public bool Delete( int userID )
        {
            return (DeleteMInfo(userID) && DeletePInfo(userID) && DeleteRelation(userID));
        }

        /// <summary>
        /// 更新用户个人信息
        /// </summary>
        /// <param name="pInfo"></param>
        /// <returns></returns>
        public bool UpDatePInfo( UserPInfo pInfo )
        {
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandStr = "UPDATE UserPInfo SET" +
                        " Name = '" + pInfo.Name +
                        "',Email = '" + pInfo.Email +
                        "',Password = '" + pInfo.Password +
                        "',UserImage = '" + pInfo.Image +
                        "',Birthday = '" + pInfo.Birthday +
                        "',Gender = " + ((int)pInfo.Gender).ToString() +
                        ",SexualOrientation = " + ((int)pInfo.SexualOrie).ToString() +
                        ",Introduction = '" + pInfo.Introduction +
                        "',Address = '" + pInfo.Address +
                        "',PrivOfEmail = " + ((int)pInfo.PrivOfEmail).ToString() +
                        ",PrivOfGender = " + ((int)pInfo.PrivOfGender).ToString() +
                        ",PrivOfAddress = " + ((int)pInfo.PrivOfAddress).ToString() +
                        ",PrivOfSexualOrie = " + ((int)pInfo.PrivOfSexualOrie).ToString() +
                        ",PrivOfBirthday = " + ((int)pInfo.PrivOfBirthday).ToString() +
                        " WHERE ID = " + pInfo.ID.ToString();
            SqlCommand sqlCom = new SqlCommand(commandStr, con);
            try
            {
                sqlCom.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception)
            {
                return false;
            }
            finally
            {
                con.Close();
            }
        }

        /// <summary>
        /// 更新用户管理信息
        /// </summary>
        /// <param name="mInfo"></param>
        /// <returns></returns>
        public bool UpDateMInfo( UserMInfo mInfo )
        {
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandStr = "UPDATE UserMInfo SET" +
                        " UserType = " + ((int)mInfo.UType).ToString() +
                        ",NewsNum= " + mInfo.NewsNum.ToString() +
                        ",CommentNum = " + mInfo.CommentNum.ToString() +
                        ",LikeNum = " + mInfo.LikeNum.ToString() +
                        ",DislikeNum = " + mInfo.DislikeNum.ToString() +
                        ",PrivOfNews = " + mInfo.PrivOfNews.ToString() +
                        ",PrivOfComment = " + mInfo.PrivOfComment.ToString() +
                        ",PrivOfMessage = " + mInfo.PrivOfMessage.ToString() +
                        ",Activation = " + mInfo.Activation.ToString() +
                        ",AuditPriv = " + mInfo.AuditPriv.ToString() +
                        ",ManageUserPriv = " + mInfo.ManageUserPriv.ToString() +
                        " WHERE ID = " + mInfo.ID.ToString();
            SqlCommand sqlCom = new SqlCommand(commandStr, con);
            try
            {
                sqlCom.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception)
            {
                return false;
            }
            finally
            {
                con.Close();
            }
        }

        

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public bool UpDate( UserInfo userInfo )
        {
            return (UpDateMInfo(userInfo.MInfo) && UpDatePInfo(userInfo.PInfo));
        }

        /// <summary>
        /// 插入新用户的管理信息
        /// </summary>
        /// <param name="mInfo"></param>
        /// <returns></returns>
        public int InsertMInfo( UserMInfo mInfo )
        {
            if (mInfo == null)
                return -1;
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandStr = "INSERT INTO UserMInfo VALUES(" + mInfo.ID.ToString()+
                        "," + ((int)mInfo.UType).ToString() + "," + mInfo.NewsNum.ToString() +
                        "," + mInfo.CommentNum.ToString() + "," + mInfo.LikeNum.ToString() +
                        "," + mInfo.DislikeNum.ToString() + "," + mInfo.PrivOfNews.ToString() +
                        "," + mInfo.PrivOfComment.ToString() + "," + mInfo.PrivOfMessage.ToString() +
                        "," + mInfo.Activation.ToString() + "," + mInfo.AuditPriv.ToString() +
                        "," + mInfo.ManageUserPriv.ToString() + ")";
            SqlCommand sqlCom = new SqlCommand(commandStr, con);
            try
            {
                sqlCom.ExecuteNonQuery();
                return mInfo.ID;
            }
            catch (System.Exception)
            {
                return -1;
            }
            finally
            {
                con.Close();
            }
        }

        /// <summary>
        /// 插入新用户的个人信息
        /// </summary>
        /// <param name="pInfo"></param>
        /// <returns></returns>
        public int InsertPInfo( UserPInfo pInfo )
        {
            if (pInfo == null)
                return -1;
	        string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandStr = "INSERT INTO dbo.UserPInfo(" +
                        "Name,Email,Password,UserImage,Birthday,Gender,SexualOrientation," +
                        "Introduction,Address,PrivOfEmail,PrivOfGender,PrivOfAddress," +
                        "PrivOfSexualOrie,PrivOfBirthday)VALUES('" +
                        pInfo.Name + "','" + pInfo.Email + "','" + pInfo.Password + "','" + pInfo.Image +
                        "','" + pInfo.Birthday + "'," + ((int)pInfo.Gender).ToString() + 
                        "," + ((int)pInfo.SexualOrie).ToString() +
                        ",'" + pInfo.Introduction + "','" + pInfo.Address +
                        "'," + ((int)pInfo.PrivOfEmail).ToString() +
                        "," + ((int)pInfo.PrivOfGender).ToString() +
                        "," + ((int)pInfo.PrivOfAddress).ToString() +
                        "," + ((int)pInfo.PrivOfSexualOrie).ToString() + 
                        "," + ((int)pInfo.PrivOfBirthday).ToString()+")";
            SqlCommand sqlCom = new SqlCommand(commandStr, con);
            try
            {
                sqlCom.ExecuteNonQuery();
                sqlCom.CommandText = "SELECT ID FROM dbo.UserPInfo WHERE Name = '" + pInfo.Name + "'";
                SqlDataReader sqlRead = sqlCom.ExecuteReader();
                sqlRead.Read();
                return (int)sqlRead["ID"];
            }
            catch (System.Exception)
            {
                return -1;
            }
            finally
            {
                con.Close();
            }
        }

        /// <summary>
        /// 插入用户个人信息
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public int Insert( UserInfo userInfo )
        {
            userInfo.PInfo.ID = InsertPInfo(userInfo.PInfo);
            userInfo.MInfo.ID = userInfo.PInfo.ID;
            InsertMInfo(userInfo.MInfo);
            return userInfo.PInfo.ID;
        }

        /// <summary>
        /// 插入用户关系
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="followID"></param>
        /// <returns></returns>
        public bool InsertRelation( int ID, int followID )
        {
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandStr = "INSERT INTO UserRelation VALUES(" + ID.ToString()
                    + "," + followID.ToString() + ")";
            SqlCommand sqlCom = new SqlCommand(commandStr, con);
            try
            {
                sqlCom.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception)
            {
                return false;
            }
            finally
            {
                con.Close();
            }
        }

        /// <summary>
        /// 根据用户ID获取用户管理信息
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public UserMInfo GetUserMInfo( int userID )
        {
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandStr = "SELECT * FROM UserMInfo WHERE ID = " + userID.ToString();
            SqlCommand sqlCom = new SqlCommand(commandStr, con);
            try
            {
                SqlDataReader sqlRead = sqlCom.ExecuteReader();
                if (sqlRead.HasRows)
                {
                    sqlRead.Read();
                    return ReadToMInfo(sqlRead);
                }
                else
                    return null;
            }
            catch (System.Exception)
            {
                return null;
            }
            finally
            {
                con.Close();
            }
        }

        /// <summary>
        /// 根据用户名称获取用户管理信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public UserMInfo GetUserMInfo( string userName )
        {
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandStr = "SELECT * FROM UserPInfo WHERE UserName = '" + userName.ToString() + "'";
            SqlCommand sqlCom = new SqlCommand(commandStr, con);
            SqlDataReader sqlRead = sqlCom.ExecuteReader();
            int userID = (int)sqlRead["ID"];
            con.Close();
            return GetUserMInfo(userID);
                
        }

        /// <summary>
        /// 根据用户ID获取用户个人信息
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public UserPInfo GetUserPInfo( int userID )
        {
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();

            string commandStr = "SELECT * FROM UserPInfo WHERE ID = " + userID.ToString();
            SqlCommand sqlCom = new SqlCommand(commandStr, con);
            try
            {
                SqlDataReader sqlRead = sqlCom.ExecuteReader();
                if (sqlRead.HasRows)
                {
                    sqlRead.Read();
                    return ReadToPInfo(sqlRead);
                }
                else
                    return null;
            }
            catch (System.Exception)
            {
                return null;
            }
            finally
            {
                con.Close();
            }
        }

        /// <summary>
        /// 根据用户名获取用户个人信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public UserPInfo GetUserPInfo( string userName )
        {
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandStr = "SELECT * FROM UserPInfo WHERE UserName = '" + userName + "'";
            SqlCommand sqlCom = new SqlCommand(commandStr, con);
            SqlDataReader sqlRead = sqlCom.ExecuteReader();
            int userID = (int)sqlRead["ID"];
            return GetUserPInfo(userID);
        }

        /// <summary>
        /// 获取userID的所有关注信息
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public UserRelation GetUserRelation(int userID)
        {
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandStr = "SELECT FollowID FROM UserRelation WHERE ID = " + userID;
            SqlCommand sqlCom = new SqlCommand(commandStr, con);
            SqlDataReader sqlRead = sqlCom.ExecuteReader();
            UserRelation relation = new UserRelation();
            while (sqlRead.Read())
            {
                relation.Follow.Add((int)sqlRead["Follow"]);
            }
            sqlCom.CommandText = "SELECT ID FROM UserRelation WHERE FollowID = " + userID;
            sqlRead.Close();
            sqlRead = sqlCom.ExecuteReader();
            while (sqlRead.Read())
            {
                relation.Fans.Add((int)sqlRead["ID"]);
            }
            return relation;
        }

        /// <summary>
        /// 获取userID的用户信息
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public UserInfo GetUserInfo( int userID )
        {
            UserInfo userInfo = new UserInfo();
            userInfo.PInfo = GetUserPInfo(userID);
            userInfo.MInfo = GetUserMInfo(userID);
            userInfo.Relation = GetUserRelation(userID);
            return userInfo;
        }

        /// <summary>
        /// 根据用户名获取用户信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public UserInfo GetUserInfo( string userName )
        {
            UserInfo userInfo = new UserInfo();
            userInfo.PInfo = GetUserPInfo(userName);
            userInfo.MInfo = GetUserMInfo(userName);
            userInfo.Relation = GetUserRelation(userInfo.PInfo.ID);
            return userInfo;
        }

        /// <summary>
        /// 从SqlDataReader读取用户管理信息
        /// </summary>
        /// <param name="sqlRead"></param>
        /// <returns></returns>
        public UserMInfo ReadToMInfo(SqlDataReader sqlRead)
        {
            UserMInfo mInfo = new UserMInfo();
            mInfo.ID = (int)sqlRead["ID"];
            mInfo.UType = (UserType)Enum.Parse(typeof(UserType), ((int)sqlRead["UserType"]).ToString());
            mInfo.NewsNum = (int)sqlRead["NewsNum"];
            mInfo.CommentNum = (int)sqlRead["CommentNum"];
            mInfo.LikeNum = (int)sqlRead["LikeNum"];
            mInfo.DislikeNum = (int)sqlRead["DislikeNum"];
            mInfo.PrivOfNews = (int)sqlRead["PrivOfNews"];
            mInfo.PrivOfComment = (int)sqlRead["PrivOfComment"];
            mInfo.PrivOfMessage = (int)sqlRead["PrivOfMessage"];
            mInfo.Activation = (int)sqlRead["Activation"];
            mInfo.AuditPriv = (int)sqlRead["AuditPriv"];
            mInfo.ManageUserPriv = (int)sqlRead["ManageUserPriv"];
            return mInfo;
        }

        /// <summary>
        /// 从SqlDataReader读取用户个人信息
        /// </summary>
        /// <param name="sqlRead"></param>
        /// <returns></returns>
        public UserPInfo ReadToPInfo(SqlDataReader sqlRead)
        {
            UserPInfo pInfo = new UserPInfo();
            pInfo.ID = (int)sqlRead["ID"];
            pInfo.Name = sqlRead["Name"].ToString();
            pInfo.Email = sqlRead["Email"].ToString();
            pInfo.Password = sqlRead["Password"].ToString();
            pInfo.Birthday = Convert.ToDateTime(sqlRead["Birthday"]).ToString("yyyy-MM-dd"); 
            pInfo.Gender = (GenderType)Enum.Parse(typeof(GenderType), sqlRead["Gender"].ToString());
            pInfo.SexualOrie = (GenderType)Enum.Parse(typeof(GenderType), sqlRead["SexualOrientation"].ToString());
            pInfo.Introduction = sqlRead["Introduction"].ToString(); 
            pInfo.Address = sqlRead["Address"].ToString(); 
            pInfo.PrivOfEmail = (PrivacyType)Enum.Parse(typeof(GenderType), sqlRead["PrivOfEmail"].ToString());
            pInfo.PrivOfGender = (PrivacyType)Enum.Parse(typeof(GenderType), sqlRead["PrivOfGender"].ToString());
            pInfo.PrivOfAddress = (PrivacyType)Enum.Parse(typeof(GenderType), sqlRead["PrivOfAddress"].ToString());
            pInfo.PrivOfSexualOrie = (PrivacyType)Enum.Parse(typeof(GenderType), sqlRead["PrivOfSexualOrie"].ToString());
            pInfo.PrivOfBirthday = (PrivacyType)Enum.Parse(typeof(GenderType), sqlRead["PrivOfBirthday"].ToString());
            pInfo.Image = sqlRead["UserImage"].ToString();
            return pInfo;
        }

        /// <summary>
        /// 获取所有用户信息
        /// </summary>
        /// <returns></returns>
        public List<UserInfo> GetAllUser()
        {
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();

            string commandStr = "SELECT * FROM UserPInfo";
            SqlCommand sqlCom = new SqlCommand(commandStr, con);
            try
            {                
                SqlDataReader sqlRead = sqlCom.ExecuteReader();
                List<UserInfo> userInfoList = new List<UserInfo>();
                while (sqlRead.Read())
                {
                    UserInfo userInfo = new UserInfo();
                    userInfo.PInfo = ReadToPInfo(sqlRead);
                    userInfo.MInfo = GetUserMInfo(userInfo.PInfo.ID);
                    userInfo.Relation = GetUserRelation(userInfo.PInfo.ID);
                    userInfoList.Add(userInfo);
                }
                return userInfoList;
            }
            catch (System.Exception)
            {
                return null;
            }
            finally
            {
                con.Close();
            }
        }
    }

    /// <summary>
    /// 对数据库中新鲜事表的操作
    /// </summary> 
    public class NewsInfoAccess : INews
    {
        public bool Delete(int newsID)
        {
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandTextDelete = "DELETE FROM News WHERE ID = @id";
            SqlCommand cmdDelete = new SqlCommand(commandTextDelete, con);
            cmdDelete.Parameters.AddWithValue("@id", newsID);
            int result = cmdDelete.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                Console.WriteLine("删除记录成功.\n");
                return true;
            }
            else
            {
                Console.WriteLine("删除记录失败.\n");
                return false;
            }            
        }

        public int Insert(News news)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandTextInsert = "INSERT INTO News(NewsContent," +
            "NewsTitle, UserBelong, NewsTime, Category, Tag, State, " +
            "LikeNum, DislikeNum, CommentNum, NewsImage" +
            ") VALUES(" +
            "@newsContent, @newsTitle, @userBelong, @newsTime, @category, @tag, @state, " +
            "@likeNum, @dislikeNum, @commentNum, @newsImage)";
            SqlCommand cmdInsert = new SqlCommand(commandTextInsert, con);
            //cmdInsert.Parameters.AddWithValue("@iD", news.Id);
            cmdInsert.Parameters.AddWithValue("@newsContent", news.Content);
            cmdInsert.Parameters.AddWithValue("@newsTitle", news.Title);
            cmdInsert.Parameters.AddWithValue("@userBelong", news.UserBelong);
            cmdInsert.Parameters.AddWithValue("@newsTime", DateTime.Now.ToShortTimeString());
            cmdInsert.Parameters.AddWithValue("@category", news.Category);
            cmdInsert.Parameters.AddWithValue("@tag", news.Tag);
            cmdInsert.Parameters.AddWithValue("@state", news.State);
            cmdInsert.Parameters.AddWithValue("@likeNum", news.LikeNum);
            cmdInsert.Parameters.AddWithValue("@dislikeNum", news.DislikeNum);
            cmdInsert.Parameters.AddWithValue("@commentNum", news.CommentNum);
            cmdInsert.Parameters.AddWithValue("@newsImage", news.Image);
            int result = cmdInsert.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                Console.WriteLine("插入记录成功.\n");
                return news.Id;
            }
            else
            {
                Console.WriteLine("插入记录失败.\n");
                return -1;
            }   
        }

        public bool Update(News news)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandTextUpdate = "UpDate News Set NewsContent = @newsContent " +
                ", NewsTitle = @newsTitle, UserBelong = @userBelong, NewsTime = @newsTime " +
                ", Category = @category, Tag = @tag, State = @state, LikeNum = @likeNum " +
                ", DislikeNum = @dislikeNum, CommentNum = @commentNum, NewsImage = @newsImage " + "WHERE ID = @iD";
            SqlCommand cmdUpdate = new SqlCommand(commandTextUpdate, con);
            cmdUpdate.Parameters.AddWithValue("@iD", news.Id);
            cmdUpdate.Parameters.AddWithValue("@newsContent", news.Content);
            cmdUpdate.Parameters.AddWithValue("@newsTitle", news.Title);
            cmdUpdate.Parameters.AddWithValue("@userBelong", news.UserBelong);
            cmdUpdate.Parameters.AddWithValue("@newsTime", news.Time);
            cmdUpdate.Parameters.AddWithValue("@category", news.Category);
            cmdUpdate.Parameters.AddWithValue("@tag", news.Tag);
            cmdUpdate.Parameters.AddWithValue("@state", news.State);
            cmdUpdate.Parameters.AddWithValue("@likeNum", news.LikeNum);
            cmdUpdate.Parameters.AddWithValue("@dislikeNum", news.DislikeNum);
            cmdUpdate.Parameters.AddWithValue("@commentNum", news.CommentNum);
            cmdUpdate.Parameters.AddWithValue("@newsImage", news.Image);
            int result = cmdUpdate.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                Console.WriteLine("更新记录成功.\n");
                return true;
            }
            else
            {
                Console.WriteLine("更新记录失败.\n");
                return false;
            }   
        }

        public bool UpdateState(int newsID, NewsStateType state)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandTextUpdate = "UpDate News Set State = @state WHERE ID = @iD";
            SqlCommand cmdUpdate = new SqlCommand(commandTextUpdate, con);
            cmdUpdate.Parameters.AddWithValue("@iD", newsID);
            cmdUpdate.Parameters.AddWithValue("@state", (int)state);
            
            int result = cmdUpdate.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                Console.WriteLine("更新记录成功.\n");
                return true;
            }
            else
            {
                Console.WriteLine("更新记录失败.\n");
                return false;
            }   
        }

        public int UpdateLike(int newsID)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            
            string commandTextUpdate = "UpDate News Set LikeNum = @likeNum WHERE ID = @iD";
            SqlCommand cmdUpdate = new SqlCommand(commandTextUpdate, con);
            cmdUpdate.Parameters.AddWithValue("@iD", newsID);
            
            News m_News = GetNews(newsID);            
            cmdUpdate.Parameters.AddWithValue("@likeNum", m_News.LikeNum+1);
            int result = cmdUpdate.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                Console.WriteLine("更新记录成功.\n");
                return m_News.LikeNum + 1;
            }
            else
            {
                Console.WriteLine("更新记录失败.\n");
                return -1;
            }   
        }

        public int UpdateDislike(int newsID)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();

            string commandTextUpdate = "UpDate News Set DislikeNum = @dislikeNum WHERE ID = @iD";
            SqlCommand cmdUpdate = new SqlCommand(commandTextUpdate, con);
            cmdUpdate.Parameters.AddWithValue("@iD", newsID);

            News m_News = GetNews(newsID);
            cmdUpdate.Parameters.AddWithValue("@dislikeNum", m_News.DislikeNum + 1);
            int result = cmdUpdate.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                Console.WriteLine("更新记录成功.\n");
                return m_News.DislikeNum + 1;
            }
            else
            {
                Console.WriteLine("更新记录失败.\n");
                return -1;
            }   
        }

        public int UpdateCommentNum(int newsID, int addNum)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();

            string commandTextUpdate = "UpDate News Set CommentNum = @commentNum WHERE ID = @iD";
            SqlCommand cmdUpdate = new SqlCommand(commandTextUpdate, con);
            cmdUpdate.Parameters.AddWithValue("@iD", newsID);

            News m_News = GetNews(newsID);
            if (m_News.CommentNum == 0 && addNum == -1)
            {
                Console.WriteLine("更新评论数错误！");
                return -1;
            }
            cmdUpdate.Parameters.AddWithValue("@commentNum", m_News.CommentNum + addNum);
            int result = cmdUpdate.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                Console.WriteLine("更新记录成功.\n");
                return m_News.DislikeNum + 1;
            }
            else
            {
                Console.WriteLine("更新记录失败.\n");
                return -1;
            } 
        }

        public List<News> GetALlNews()
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            List<News> m_News = new List<News>();
            News tempm_News = new News();

            string commandTextQuery = "SELECT * FROM News";
            SqlCommand cmdQuery = new SqlCommand(commandTextQuery, con);
            SqlDataReader reader = cmdQuery.ExecuteReader();
            while (reader.Read())
            {
                tempm_News.Id = (int)reader["ID"];
                tempm_News.Content = reader["NewsContent"].ToString();
                tempm_News.Title = reader["NewsTitle"].ToString();
                tempm_News.UserBelong = (int)reader["UserBelong"];
                tempm_News.Time = reader["NewsTime"].ToString();
                tempm_News.Tag = reader["Tag"].ToString();
                tempm_News.State = (NewsStateType)Enum.Parse(typeof(NewsStateType), reader["State"].ToString());
                tempm_News.LikeNum = (int)reader["LikeNum"];
                tempm_News.DislikeNum = (int)reader["DislikeNum"];
                tempm_News.CommentNum = (int)reader["CommentNum"];
                tempm_News.Image = reader["NewsImage"].ToString();
                m_News.Add(tempm_News);
                //tempm_News.Image      
            }
            reader.Close();
            con.Close();
            return m_News;  
        }

        public List<News> GetNewsByUser(int userID)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            List<News> m_News = new List<News>();
            News tempm_News = new News();

            string commandTextQuery = "SELECT * FROM News WHERE UserBelong = @userBelong";
            SqlCommand cmdQuery = new SqlCommand(commandTextQuery, con);
            cmdQuery.Parameters.AddWithValue("@userBelong", userID);
            SqlDataReader reader = cmdQuery.ExecuteReader();
            while (reader.Read())
            {
                tempm_News.Id = (int)reader["ID"];
                tempm_News.Content = reader["NewsContent"].ToString();
                tempm_News.Title = reader["NewsTitle"].ToString();
                tempm_News.UserBelong = (int)reader["UserBelong"];
                tempm_News.Time = reader["NewsTime"].ToString();
                //tempm_News.Category = 
                tempm_News.Tag = reader["Tag"].ToString();
                tempm_News.State = (NewsStateType)Enum.Parse(typeof(NewsStateType), reader["State"].ToString());
                tempm_News.LikeNum = (int)reader["LikeNum"];
                tempm_News.DislikeNum = (int)reader["DislikeNum"];
                tempm_News.CommentNum = (int)reader["CommentNum"];
                tempm_News.Image = reader["NewsImage"].ToString();
                m_News.Add(tempm_News);
                //tempm_News.Image  
            }
            reader.Close();      
            con.Close();
            return m_News;   
        }

        public News GetNews(int getID)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();

            string commandTextQuery = "SELECT * FROM News WHERE ID = @iD";
            SqlCommand cmdQuery = new SqlCommand(commandTextQuery, con);
            cmdQuery.Parameters.AddWithValue("@iD", getID);

            SqlDataReader reader = cmdQuery.ExecuteReader();
            
            News m_News = new News();
            m_News.Id = getID;
            reader.Read();
            m_News.Content = reader["NewsContent"].ToString();
            m_News.Title = reader["NewsTitle"].ToString();
            m_News.UserBelong = (int)reader["UserBelong"];
            m_News.Time = reader["NewsTime"].ToString();
            //m_News.Category = 
            m_News.Tag = reader["Tag"].ToString();
            m_News.State = (NewsStateType)Enum.Parse(typeof(NewsStateType), reader["State"].ToString());
            m_News.LikeNum = (int)reader["LikeNum"];
            m_News.DislikeNum = (int)reader["DislikeNum"];
            m_News.CommentNum = (int)reader["CommentNum"];
            m_News.Image = reader["NewsImage"].ToString();           
            con.Close();
            return m_News;            
        }

        public List<News> GetUnauditNews()
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();

            string commandTextQuery = "SELECT * FROM News WHERE State = '1' ORDER BY NewsTime";
            SqlCommand cmdQuery = new SqlCommand(commandTextQuery, con);

            SqlDataReader reader = cmdQuery.ExecuteReader();
            List<News> listNews = new List<News>();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    News m_News = new News();
                    m_News.Id = (int)reader["id"];
                    m_News.Content = reader["NewsContent"].ToString();
                    m_News.Title = reader["NewsTitle"].ToString();
                    m_News.UserBelong = (int)reader["UserBelong"];
                    m_News.Time = reader["NewsTime"].ToString();
                    //m_News.Category = 
                    m_News.Tag = reader["Tag"].ToString();
                    m_News.State = (NewsStateType)Enum.Parse(typeof(NewsStateType), reader["State"].ToString());
                    m_News.LikeNum = (int)reader["LikeNum"];
                    m_News.DislikeNum = (int)reader["DislikeNum"];
                    m_News.CommentNum = (int)reader["CommentNum"];
                    m_News.Image = reader["NewsImage"].ToString();
                    listNews.Add(m_News);
                }      
            }
            con.Close();
            return listNews;  
        }
    }

    public class CommentAccess : IComment
    {
        public bool Delete(int commentID)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandTextDelete = "DELETE FROM Comment WHERE ID = @id";
            SqlCommand cmdDelete = new SqlCommand(commandTextDelete, con);
            cmdDelete.Parameters.AddWithValue("@id", commentID);
            int result = cmdDelete.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                Console.WriteLine("删除记录成功.\n");
                return true;
            }
            else
            {
                Console.WriteLine("删除记录失败.\n");
                return false;
            }            
        }

        public bool DeleteByUser(int userID)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandTextDelete = "DELETE FROM Comment WHERE UserBelong = @userBelong";
            SqlCommand cmdDelete = new SqlCommand(commandTextDelete, con);
            cmdDelete.Parameters.AddWithValue("@userBelong", userID);
            int result = cmdDelete.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                Console.WriteLine("删除记录成功.\n");
                return true;
            }
            else
            {
                Console.WriteLine("删除记录失败.\n");
                return false;
            }            
        }

        public bool DeleteByNews(int newsID)
        {
            //throw new Exception("The method or operation is not implemented.");
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandTextDelete = "DELETE FROM Comment WHERE NewsBelong = @newsBelong";
            SqlCommand cmdDelete = new SqlCommand(commandTextDelete, con);
            cmdDelete.Parameters.AddWithValue("@newsBelong", newsID);
            int result = cmdDelete.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                Console.WriteLine("删除记录成功.\n");
                return true;
            }
            else
            {
                Console.WriteLine("删除记录失败.\n");
                return false;
            }     
        }

        public int Insert(Comment newComment)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandTextInsert = "INSERT INTO Comment(CommentContent,NewsBelong,UserBelong,CommentBelong,CommentTime,IsRead)" +
            "VALUES(@commentContent,@newsBelong,@userBelong,@commentBelong,@commentTime,@isRead)";
            SqlCommand cmdInsert = new SqlCommand(commandTextInsert, con);
            cmdInsert.Parameters.AddWithValue("@commentContent", newComment.Content);
            cmdInsert.Parameters.AddWithValue("@newsBelong", newComment.NewsBelong);
            cmdInsert.Parameters.AddWithValue("@userBelong", newComment.UserBelong);
            cmdInsert.Parameters.AddWithValue("@commentBelong", newComment.CommentBelong);
            cmdInsert.Parameters.AddWithValue("@commentTime", DateTime.Now.ToShortTimeString());
            cmdInsert.Parameters.AddWithValue("@isRead", newComment.IsRead);           
            int result = cmdInsert.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                Console.WriteLine("插入记录成功.\n");
                return newComment.Id;
            }
            else
            {
                Console.WriteLine("插入记录失败.\n");
                return -1;
            }   
        }

        public bool UpdateIsRead(int state, int commentID)/*state = 1:新鲜事作者正在阅读；2：评论的对象正在阅读*/
        {
            //isRead = 0:两者都没读；1：新鲜事作者已读评论对象未读；2：新鲜事作者未读评论对象已读；3：两者都读
            if (state != 1 && state != 2)
                return false;
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            
            string commandTextUpdate = "UpDate Comment Set IsRead = @isRead WHERE ID = @iD";
            SqlCommand cmdUpdate = new SqlCommand(commandTextUpdate, con);
            cmdUpdate.Parameters.AddWithValue("@iD", commentID);
            
            Comment m_Comment = GetComment(commentID);
            int tempRead;
            switch (m_Comment.IsRead)
            {
            case 0:
                tempRead = state;
            	break;
            case 1:
                tempRead = (state == 1) ? 1 : 3;
            	break;
            case 2:
                tempRead = (state == 1) ? 3 : 2;
            	break;
            case 3:
                tempRead = 3;
            	break;
            default:
                return false;
            }
            cmdUpdate.Parameters.AddWithValue("@isRead", tempRead);
            int result = cmdUpdate.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                Console.WriteLine("更新记录成功.\n");
                return true;
            }
            else
            {
                Console.WriteLine("更新记录失败.\n");
                return false;
            }   
        }

        public List<Comment> GetAllComment()
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            List<Comment> m_Comment = new List<Comment>();
            Comment tempm_Comment = new Comment();

            string commandTextQuery = "SELECT * FROM Comment";
            SqlCommand cmdQuery = new SqlCommand(commandTextQuery, con);
            SqlDataReader reader = cmdQuery.ExecuteReader();
            while (reader.Read())
            {
                tempm_Comment.Id = (int)reader["ID"];
                tempm_Comment.Content = reader["CommentContent"].ToString();
                tempm_Comment.NewsBelong = (int)reader["NewsBelong"];
                tempm_Comment.UserBelong = (int)reader["UserBelong"];
                tempm_Comment.CommentBelong = (int)reader["CommentBelong"];
                tempm_Comment.Time = reader["CommentTime"].ToString();
                tempm_Comment.IsRead = (int)reader["IsRead"];
                m_Comment.Add(tempm_Comment);
                //tempm_News.Image  m_News News     
            }
            reader.Close();
            con.Close();
            return m_Comment;  
        }

        public List<Comment> GetCommentByUser(int userID)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            List<Comment> m_Comment = new List<Comment>();
            Comment tempm_Comment = new Comment();

            string commandTextQuery = "SELECT * FROM Comment WHERE UserBelong = @userBelong";
            SqlCommand cmdQuery = new SqlCommand(commandTextQuery, con);
            cmdQuery.Parameters.AddWithValue("@userBelong", userID);
            SqlDataReader reader = cmdQuery.ExecuteReader();
            while (reader.Read())
            {
                tempm_Comment.Id = (int)reader["ID"];
                tempm_Comment.Content = reader["CommentContent"].ToString();
                tempm_Comment.NewsBelong = (int)reader["NewsBelong"];
                tempm_Comment.UserBelong = (int)reader["UserBelong"];
                tempm_Comment.CommentBelong = (int)reader["CommentBelong"];
                tempm_Comment.Time = reader["CommentTime"].ToString();
                tempm_Comment.IsRead = (int)reader["IsRead"];
                m_Comment.Add(tempm_Comment);
                //tempm_News.Image  
            }
            reader.Close();
            con.Close();
            return m_Comment;   
        }

        public List<Comment> GetCommentByNews(int newsID)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            List<Comment> m_Comment = new List<Comment>();
            Comment tempm_Comment = new Comment();

            string commandTextQuery = "SELECT * FROM Comment WHERE NewsBelong = @newsBelong";
            SqlCommand cmdQuery = new SqlCommand(commandTextQuery, con);
            cmdQuery.Parameters.AddWithValue("@newsBelong", newsID);
            SqlDataReader reader = cmdQuery.ExecuteReader();
            while (reader.Read())
            {
                tempm_Comment.Id = (int)reader["ID"];
                tempm_Comment.Content = reader["CommentContent"].ToString();
                tempm_Comment.NewsBelong = (int)reader["NewsBelong"];
                tempm_Comment.UserBelong = (int)reader["UserBelong"];
                tempm_Comment.CommentBelong = (int)reader["CommentBelong"];
                tempm_Comment.Time = reader["CommentTime"].ToString();
                tempm_Comment.IsRead = (int)reader["IsRead"];
                m_Comment.Add(tempm_Comment);
                //tempm_News.Image  
            }
            reader.Close();
            con.Close();
            return m_Comment;   
        }

        public Comment GetComment(int commentID)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();

            string commandTextQuery = "SELECT * FROM Comment WHERE ID = @iD";
            SqlCommand cmdQuery = new SqlCommand(commandTextQuery, con);
            cmdQuery.Parameters.AddWithValue("@iD", commentID);

            SqlDataReader reader = cmdQuery.ExecuteReader();
            Comment m_Comment = new Comment();
            reader.Read();
            m_Comment.Id = (int)reader["ID"];
            m_Comment.Content = reader["CommentContent"].ToString();
            m_Comment.NewsBelong = (int)reader["NewsBelong"];
            m_Comment.UserBelong = (int)reader["UserBelong"];
            m_Comment.CommentBelong = (int)reader["CommentBelong"];
            m_Comment.Time = reader["CommentTime"].ToString();            
            m_Comment.IsRead = (int)reader["IsRead"];
           
            con.Close();
            return m_Comment;  
        }
    }

    public class MessageAccess : IMessage
    {        
        public bool Delete(int messageID)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandTextDelete = "DELETE FROM Message WHERE ID = @id";
            SqlCommand cmdDelete = new SqlCommand(commandTextDelete, con);
            cmdDelete.Parameters.AddWithValue("@id", messageID);
            int result = cmdDelete.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                Console.WriteLine("删除记录成功.\n");
                return true;
            }
            else
            {
                Console.WriteLine("删除记录失败.\n");
                return false;
            }            
        }

        public bool DeleteBySend(int sendID)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandTextDelete = "DELETE FROM Message WHERE SendID = @sendID";
            SqlCommand cmdDelete = new SqlCommand(commandTextDelete, con);
            cmdDelete.Parameters.AddWithValue("@sendID", sendID);
            int result = cmdDelete.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                Console.WriteLine("删除记录成功.\n");
                return true;
            }
            else
            {
                Console.WriteLine("删除记录失败.\n");
                return false;
            }            
        }

        public bool DeleteByReceive(int receiveID)
        {
            //throw new Exception("The method or operation is not implemented.");
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandTextDelete = "DELETE FROM Message WHERE ReceiveID = @receiveID";
            SqlCommand cmdDelete = new SqlCommand(commandTextDelete, con);
            cmdDelete.Parameters.AddWithValue("@receiveID", receiveID);
            int result = cmdDelete.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                Console.WriteLine("删除记录成功.\n");
                return true;
            }
            else
            {
                Console.WriteLine("删除记录失败.\n");
                return false;
            }
        }

        public bool Insert(Message newMessage)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandTextInsert = "INSERT INTO Message(SendID,ReceiveID,SendTime,MessageContent,IsRead)" +
            "VALUES(@sendID,@receiveID,@sendTime,@messageContent,@isRead)";
            SqlCommand cmdInsert = new SqlCommand(commandTextInsert, con);
            cmdInsert.Parameters.AddWithValue("@sendID", newMessage.SendID);
            cmdInsert.Parameters.AddWithValue("@receiveID", newMessage.ReceiveID);
            cmdInsert.Parameters.AddWithValue("@sendTime", DateTime.Now.ToShortTimeString());
            cmdInsert.Parameters.AddWithValue("@messageContent", newMessage.Content);
            cmdInsert.Parameters.AddWithValue("@isRead", newMessage.IsRead);
            int result = cmdInsert.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                Console.WriteLine("插入记录成功.\n");
                return true;
            }
            else
            {
                Console.WriteLine("插入记录失败.\n");
                return false;
            }   
        }

        public bool UpdateIsRead(int messageID)
        {
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();

            string commandTextUpdate = "UpDate Message Set IsRead = @isRead WHERE ID = @iD";
            SqlCommand cmdUpdate = new SqlCommand(commandTextUpdate, con);
            cmdUpdate.Parameters.AddWithValue("@iD", messageID);

            Message m_Message = GetMessage(messageID);
            if (m_Message.IsRead == true)
                return true;            
            cmdUpdate.Parameters.AddWithValue("@isRead", 1);
            int result = cmdUpdate.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                Console.WriteLine("更新记录成功.\n");
                return true;
            }
            else
            {
                Console.WriteLine("更新记录失败.\n");
                return false;
            }   
        }

        public List<Message> GetAllMessage()
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            List<Message> m_message = new List<Message>();
            Message tempm_Message = new Message();

            string commandTextQuery = "SELECT * FROM Message";
            SqlCommand cmdQuery = new SqlCommand(commandTextQuery, con);
            SqlDataReader reader = cmdQuery.ExecuteReader();
            while (reader.Read())
            {
                tempm_Message.Id = (int)reader["ID"];
                tempm_Message.Content = reader["MessageContent"].ToString();
                tempm_Message.SendID = (int)reader["SendID"];
                tempm_Message.ReceiveID = (int)reader["ReceiveID"];
                tempm_Message.Time = reader["SendTime"].ToString();
                tempm_Message.IsRead = ((int)reader["IsRead"] == 1);
                m_message.Add(tempm_Message);
                //tempm_News.Image  m_News News     
            }
            reader.Close();
            con.Close();
            return m_message;  
        }
                
        public List<Message> GetMessageBySend(int userID)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            List<Message> m_message = new List<Message>();
            Message tempm_Message = new Message();

            string commandTextQuery = "SELECT * FROM Message WHERE SendID = @sendID";
            SqlCommand cmdQuery = new SqlCommand(commandTextQuery, con);
            cmdQuery.Parameters.AddWithValue("@sendID", userID);
            SqlDataReader reader = cmdQuery.ExecuteReader();
            while (reader.Read())
            {
                tempm_Message.Id = (int)reader["ID"];
                tempm_Message.Content = reader["MessageContent"].ToString();
                tempm_Message.SendID = (int)reader["SendID"];
                tempm_Message.ReceiveID = (int)reader["ReceiveID"];
                tempm_Message.Time = reader["SendTime"].ToString();
                tempm_Message.IsRead = ((int)reader["IsRead"] == 1);
                m_message.Add(tempm_Message);
                //tempm_News.Image  
            }
            reader.Close();
            con.Close();
            return m_message;  

        }

        public List<Message> GetMessageByReceive(int userID)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            List<Message> m_message = new List<Message>();
            Message tempm_Message = new Message();

            string commandTextQuery = "SELECT * FROM Message WHERE ReceiveID = @receiveID";
            SqlCommand cmdQuery = new SqlCommand(commandTextQuery, con);
            cmdQuery.Parameters.AddWithValue("@receiveID", userID);
            SqlDataReader reader = cmdQuery.ExecuteReader();
            while (reader.Read())
            {
                tempm_Message.Id = (int)reader["ID"];
                tempm_Message.Content = reader["MessageContent"].ToString();
                tempm_Message.SendID = (int)reader["SendID"];
                tempm_Message.ReceiveID = (int)reader["ReceiveID"];
                tempm_Message.Time = reader["SendTime"].ToString();
                tempm_Message.IsRead = ((int)reader["IsRead"] == 1);
                m_message.Add(tempm_Message);
                //tempm_News.Image  
            }
            reader.Close();
            con.Close();
            return m_message;  
        }

        public List<Message> GetMessageByUser(int sendID, int receiveID)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            List<Message> m_message = new List<Message>();
            Message tempm_Message = new Message();

            string commandTextQuery = "SELECT * FROM Message WHERE ReceiveID = @receiveID AND SendID = @sendID";
            SqlCommand cmdQuery = new SqlCommand(commandTextQuery, con);
            cmdQuery.Parameters.AddWithValue("@receiveID", receiveID);
            cmdQuery.Parameters.AddWithValue("@sendID", sendID);
            SqlDataReader reader = cmdQuery.ExecuteReader();
            while (reader.Read())
            {
                tempm_Message.Id = (int)reader["ID"];
                tempm_Message.Content = reader["MessageContent"].ToString();
                tempm_Message.SendID = (int)reader["SendID"];
                tempm_Message.ReceiveID = (int)reader["ReceiveID"];
                tempm_Message.Time = reader["SendTime"].ToString();
                tempm_Message.IsRead = ((int)reader["IsRead"] == 1);
                m_message.Add(tempm_Message);
                //tempm_News.Image  
            }
            reader.Close();
            con.Close();
            return m_message;
        }

        public Message GetMessage(int messageID)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();

            string commandTextQuery = "SELECT * FROM Message WHERE ID = @iD";
            SqlCommand cmdQuery = new SqlCommand(commandTextQuery, con);
            cmdQuery.Parameters.AddWithValue("@iD", messageID);

            SqlDataReader reader = cmdQuery.ExecuteReader();
            Message m_Message = new Message();
            reader.Read();
            m_Message.Id = (int)reader["ID"];
            m_Message.Content = reader["MessageContent"].ToString();
            m_Message.SendID = (int)reader["SendID"];
            m_Message.ReceiveID = (int)reader["ReceiveID"];
            m_Message.Time = reader["SendTime"].ToString();
            m_Message.IsRead = ((int)reader["IsRead"] == 1);

            con.Close();
            return m_Message;  
        }
    }

    public class HistoryAccess : IHistory
    {
        public bool Delete(int historyID)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandTextDelete = "DELETE FROM ManageHistory WHERE ID = @id";
            SqlCommand cmdDelete = new SqlCommand(commandTextDelete, con);
            cmdDelete.Parameters.AddWithValue("@id",historyID);
            int result = cmdDelete.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                Console.WriteLine("删除记录成功.\n");
                return true;
            }
            else
            {
                Console.WriteLine("删除记录失败.\n");
                return false;
            }            
        }

        public bool Insert(ManageHistory newHistory)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            string commandTextInsert = "INSERT INTO ManageHistory(Administrator,Action,ObjectUser,Object,HistoryTime)" +
            "VALUES(@administrator,@action,@objectUser,@object,@historyTime)";
            SqlCommand cmdInsert = new SqlCommand(commandTextInsert, con);
            cmdInsert.Parameters.AddWithValue("@administrator", newHistory.Administrator);
            cmdInsert.Parameters.AddWithValue("@action", newHistory.Action);
            cmdInsert.Parameters.AddWithValue("@objectUser", newHistory.ObjectUser);
            cmdInsert.Parameters.AddWithValue("@object", newHistory.ManageObject);
            cmdInsert.Parameters.AddWithValue("@historyTime", DateTime.Now.ToShortTimeString());
            int result = cmdInsert.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                Console.WriteLine("插入记录成功.\n");
                return true;
            }
            else
            {
                Console.WriteLine("插入记录失败.\n");
                return false;
            }   
        }

        public List<ManageHistory> GetAllHistory()
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            List<ManageHistory> m_manageHistory = new List<ManageHistory>();
            ManageHistory tempm_ManageHistory = new ManageHistory();

            string commandTextQuery = "SELECT * FROM ManageHistory";
            SqlCommand cmdQuery = new SqlCommand(commandTextQuery, con);
            SqlDataReader reader = cmdQuery.ExecuteReader();
            while (reader.Read())
            {
                tempm_ManageHistory.Id = (int)reader["ID"];
                tempm_ManageHistory.Administrator = (int)reader["Administrator"];
                tempm_ManageHistory.ObjectUser = (int)reader["ObjectUser"];
                tempm_ManageHistory.Time = reader["HistoryTime"].ToString();
                tempm_ManageHistory.Action = (ActionType)reader["Action"];
                tempm_ManageHistory.ManageObject = (ObjectType)reader["Object"];
                m_manageHistory.Add(tempm_ManageHistory);
                //tempm_News.Image  m_News News     
            }
            reader.Close();
            con.Close();
            return m_manageHistory;  
        }

        public List<ManageHistory> GetHistoryByUser(int userID)//管理员
        {            
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            List<ManageHistory> m_manageHistory = new List<ManageHistory>();
            ManageHistory tempm_ManageHistory = new ManageHistory();

            string commandTextQuery = "SELECT * FROM ManageHistory WHERE Administrator = @administrator";
            SqlCommand cmdQuery = new SqlCommand(commandTextQuery, con);
            cmdQuery.Parameters.AddWithValue("@administrator", userID);
            SqlDataReader reader = cmdQuery.ExecuteReader();
            while (reader.Read())
            {
                tempm_ManageHistory.Id = (int)reader["ID"];
                tempm_ManageHistory.Administrator = (int)reader["Administrator"];
                tempm_ManageHistory.ObjectUser = (int)reader["ObjectUser"];
                tempm_ManageHistory.Time = reader["HistoryTime"].ToString();
                tempm_ManageHistory.Action = (ActionType)reader["Action"];
                tempm_ManageHistory.ManageObject = (ObjectType)reader["Object"];
                m_manageHistory.Add(tempm_ManageHistory);
                //tempm_News.Image  m_News News     
            }
            reader.Close();
            con.Close();
            return m_manageHistory;  
        }

        public List<ManageHistory> GetHistoryByObject(ObjectType manageObject)//管理对象
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            List<ManageHistory> m_manageHistory = new List<ManageHistory>();
            ManageHistory tempm_ManageHistory = new ManageHistory();

            string commandTextQuery = "SELECT * FROM ManageHistory WHERE Object = @object";
            SqlCommand cmdQuery = new SqlCommand(commandTextQuery, con);
            cmdQuery.Parameters.AddWithValue("@object", manageObject);
            SqlDataReader reader = cmdQuery.ExecuteReader();
            while (reader.Read())
            {
                tempm_ManageHistory.Id = (int)reader["ID"];
                tempm_ManageHistory.Administrator = (int)reader["Administrator"];
                tempm_ManageHistory.ObjectUser = (int)reader["ObjectUser"];
                tempm_ManageHistory.Time = reader["HistoryTime"].ToString();
                tempm_ManageHistory.Action = (ActionType)reader["Action"];
                tempm_ManageHistory.ManageObject = (ObjectType)reader["Object"];
                m_manageHistory.Add(tempm_ManageHistory);
                //tempm_News.Image  m_News News     
            }
            reader.Close();
            con.Close();
            return m_manageHistory;  
        }

        public List<ManageHistory> GetHistoryByObjectUser(int userID)//被管理人
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();
            List<ManageHistory> m_manageHistory = new List<ManageHistory>();
            ManageHistory tempm_ManageHistory = new ManageHistory();

            string commandTextQuery = "SELECT * FROM ManageHistory WHERE ObjectUser = @objectUser";
            SqlCommand cmdQuery = new SqlCommand(commandTextQuery, con);
            cmdQuery.Parameters.AddWithValue("@objectUser", userID);
            SqlDataReader reader = cmdQuery.ExecuteReader();
            while (reader.Read())
            {
                tempm_ManageHistory.Id = (int)reader["ID"];
                tempm_ManageHistory.Administrator = (int)reader["Administrator"];
                tempm_ManageHistory.ObjectUser = (int)reader["ObjectUser"];
                tempm_ManageHistory.Time = reader["HistoryTime"].ToString();
                tempm_ManageHistory.Action = (ActionType)reader["Action"];
                tempm_ManageHistory.ManageObject = (ObjectType)reader["Object"];
                m_manageHistory.Add(tempm_ManageHistory);
                //tempm_News.Image  m_News News     
            }
            reader.Close();
            con.Close();
            return m_manageHistory;  
        }

        public ManageHistory GetHistory(int historyID)
        {
            //throw new Exception("The method or operation is not implemented.");
            string sqlStr = ConfigurationManager.AppSettings["conStr"];
            SqlConnection con = new SqlConnection(sqlStr);
            con.Open();

            string commandTextQuery = "SELECT * FROM ManageHistory WHERE ID = @iD";
            SqlCommand cmdQuery = new SqlCommand(commandTextQuery, con);
            cmdQuery.Parameters.AddWithValue("@iD", historyID);

            SqlDataReader reader = cmdQuery.ExecuteReader();
            ManageHistory m_ManageHistory = new ManageHistory();
            reader.Read();
            m_ManageHistory.Id = (int)reader["ID"];
            m_ManageHistory.Administrator = (int)reader["Administrator"];
            m_ManageHistory.ObjectUser = (int)reader["ObjectUser"];
            m_ManageHistory.Time = reader["HistoryTime"].ToString();
            m_ManageHistory.Action = (ActionType)reader["Action"];
            m_ManageHistory.ManageObject = (ObjectType)reader["Object"];
            con.Close();
            return m_ManageHistory;  
        }
    }
}